﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XiDainChangAn.OA.IBLL;
using XiDainChangAn.OA.WebApp.Models;
using XiDianChangAn.OA.Model;

namespace XiDainChangAn.OA.WebApp.Controllers
{
    [MyLoginCheckFilterAttribute(IsCheck = false)]
    public class WFTempController : Controller
    {
        public IWF_TempService WF_TempService { get; set; }
        // GET: WFTemp

        public ActionResult Index()
        {
            return View();
        }

        #region 01，展示所有的流程模板+JsonResult ShowAllWFTemp()
        /// <summary>
        /// 展示所有的流程模板
        /// </summary>
        /// <returns></returns>
        public JsonResult ShowAllWFTemp()
        {
            int pageInde = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize = Request["rows"] == null ? 1 : int.Parse(Request["rows"]);
            int totalCount;
            var tempList = WF_TempService.GetPageEntitys<long>(pageSize, pageInde, out totalCount, true, u => true, u => u.Id).ToList();
            var temp = from u in tempList
                       select new
                       {
                           Id = u.Id,
                           TempName = u.TemName,
                           ActityType = u.ActityType,
                           Desc = u.Desc,
                           Remark = u.Remark
                       };
            //客户端就是遍历rows  jq easy-ui就是遍历rows
            return Json(new { rows = temp, total = totalCount }, JsonRequestBehavior.AllowGet);
        } 
        #endregion

        #region 02，新增流程模板
        /// <summary>
        /// 新增流程模板
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddTemp(WF_Temp model)
        {
            model.DelFlag = (short)XiDianChangAn.OA.Model.Enum.DelFlagEnum.Normal;
            model.SubTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            WF_TempService.Add(model);
            return Content("ok");
        } 
        #endregion

        #region 03，编辑模板信息
        /// <summary>
        /// 获取便编辑的模板信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            var model = WF_TempService.GetById(id);
            return Json(model, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 编辑模板信息
        /// </summary>
        /// <param name="model">模板实体</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(WF_Temp model)
        {
            if (ModelState.IsValid)
            {
                WF_TempService.UpDate(model);
            }
            return Content("ok");
        } 
        #endregion

        #region 04，删除模板信息
        /// <summary>
        /// 删除模板信息
        /// </summary>
        /// <param name="strId"></param>
        /// <returns></returns>
        public ActionResult Delete(string strId)
        {
            string[] str = strId.Split(new char[] { ',' });
            foreach (string item in str)
            {
                WF_TempService.Delete(WF_TempService.GetById(int.Parse(item)));
            }
            return Content("ok");
        } 
        #endregion


        public ActionResult StartWF()
        {
            ViewData.Model = WF_TempService.GetEntitys(u => true);
            return View();
        }
    }
}